<template>
  <div class="page-container">
    <div class="page-header">
      <div class="page-tile">
        {{ $t('route.systemSettings') }}
        <i v-if="false" class="iconfont iconzhongyiicon_gaojing"></i>
      </div>
      <div class="left-wrapper"></div>
      <div class="right-wrapper"></div>
    </div>
    <el-scrollbar class="page-content">
      <div class="content-wrapper">
        <el-card>
          <div class="action-wrapper">
            <el-button v-if="!optionEditable" :disabled="tenantLoading" @click="optionEditable = true" class="icon-button setting-button" style="width: auto;">
                <i class="el-icon-edit" style="font-size: 12px; margin-right: 5px;"></i>
                <span style="font-size: 12px;">{{$t('common.edit')}}</span>
              </el-button>
              <el-button v-if="optionEditable" type="primary" :disabled="tenantLoading" @click="save()" class="icon-button setting-button" style="width: auto;" >
                <i class="el-icon-check" style="font-size: 12px; margin-right: 5px;"></i>
                <span style="font-size: 12px;">{{$t('common.save')}}</span>
              </el-button>
              <el-button v-if="optionEditable" :disabled="tenantLoading" @click="cancel()" class="icon-button setting-button" style="width: auto;">
                <i class="el-icon-close" style="font-size: 12px; margin-right: 5px;"></i>
                <span style="font-size: 12px;">{{$t('common.cancel')}}</span>
              </el-button>
            </div>
            <el-form ref="tenantOptionForm" label-width="180" :model="tenantOptionData" :rules="tenantOptionRules" v-loading="tenantLoading" class="options-list-container">
                <el-form-item :label="$t('systemSettings.desktop_client_console_password')" prop="console_password" class="option-container">
                  <span v-if="!optionEditable">{{ getPasswordHidden(tenantOptionData.console_password) }}</span>
                  <el-input type="password" show-password v-if="optionEditable" v-model="tenantOptionData.console_password" @keyup.enter.native="save"></el-input>
                </el-form-item>
            </el-form>
        </el-card>
      </div>
    </el-scrollbar>
  </div>
</template>
<script lang="ts">
import Vue from 'vue'
import BasicTable from '../../components/Basic/Table/BasicTable.vue';
import LinkButton from '../../components/Basic/Button/LinkButton.vue';
import { AppModule } from '@/store/modules/app';
import { encrypt,decodeToken } from '@/utils/auth';
export default Vue.extend({
  components: {
    BasicTable,
    LinkButton
  },
  data() {
    return {
      language: AppModule.language,
      optionEditable: false,
      tenantLoading: false,
      tenantOptionData: {
        console_password: ''
      },
      tenantOptionRules: {
        console_password: [
          { required: true, message: '请输入终端高级设置密码', trigger: 'blur' },
        ],
      }

    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    // 密码变*
    getPasswordHidden(password: any) {
      if(!password) {
        return password
      }
      let str = '';
      for (let i = 0; i < password.length; i ++) {
        str += '*'
      }
      return str;
    },
    getData() {
      this.$pandaApi.apiGetTenantSetting().then((res: any) => {
        if(res.length > 0) {
          this.tenantOptionData.console_password = decodeToken(res[0].console_password.replace('\n','')); // 终端高级设置密码
        }else {
          this.tenantOptionData.console_password = ''
        }
        
      }).catch((err: any) => {

      })
    },
    async save() {
      (this as any).$refs['tenantOptionForm'].validate((valid: any, something: any) => {
        if (valid) {
          this.$pandaApi.apiPatchTenantConsolePassword({
            "console_password": encrypt((this as any).tenantOptionData.console_password)
          }).then((res: any) => {
            this.$message.success((this as any).$t('common.changeSuccess'));
            this.optionEditable = false;
            (this as any).$refs['tenantOptionForm'].clearValidate()
            this.getData();
          }).catch((err: any) => {
            this.$message.warning((this as any).$t('common.changeError'));
            this.optionEditable = false;
          })
        }
      });
    },
    cancel() {
      this.optionEditable = false;
      (this as any).$refs['tenantOptionForm'].clearValidate()
    }
  }
})
</script>
<style lang="scss" scoped>
.page-container {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: #EFF1F4;

  .page-header {
    height: 60px;
    background-color: #FFFFFF;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #D9D9D9;

    .page-tile {
      color: #031129;
      font-size: 16px;
      margin-left: 20px;

      .iconzhongyiicon_gaojing {
        margin-left: 5px;
      }
    }
  }

  .page-content {
    height: calc(100vh - 110px);
    overflow: hidden;
    flex-grow: 1;

    .content-wrapper {
      padding: 15px;
    }
  }
}

.table-left-options {
  display: flex;
}

.table-right-options {
  display: flex;

  .setting-button {
    margin-right: 10px;
  }
}

.delete-warning-icon {
  color: #FF931D;
  font-size: 20px;
  margin-right: 10px;

  &::before {
    font-size: 20px;
  }
}

.action-wrapper {
  margin-bottom: 15px;
  display: flex;
}

.option-container {
  width: 70%;
  display: flex;
  margin: 0px;
  border-top: 1px solid #D9D9D9;

  ::v-deep .el-form-item__label {
    font-size: 14px;
    font-weight: unset;
    text-align: left;
    width: 315px;
    flex-shrink: 0;
    padding: 8px 12px;
    background-color: #F9F9F9;
  }

  ::v-deep .el-form-item__content {
    padding-left: 15px;
    flex-grow: 1;
    display: flex;
    justify-content: left;
    align-items: center;

    span {
      width: 320px;
      display: block;
    }

    .el-input, .el-select {
      width: 40%;
      flex-shrink: 0;
    }

    .el-form-item__error {
      margin-left: 5px;
      flex-grow: 1;
      position: unset;
    }
  }
}
.option-container {
  ::v-deep .el-input__icon.el-icon-view.el-input__clear {
    position: absolute;
    right: 0;
  }
}
.option-container:last-child {
  border-bottom: 1px solid #D9D9D9;
}
</style>
